package com.hymjweb.demo.hymj.basic_data_manage.xt_city.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.bean.XtCityCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.bean.XtCityPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_city.service.XtCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/basic_data_manage/xt_city")
public class XtCityController extends BaseController {

    @Autowired
    private XtCityService xtCityService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/basic_data_manage/xt_city/xtcity";
    }

    @RequestMapping("/findCacheAll")
    @ResponseBody
    public List<XtCityCachePojo> findCacheAll(@RequestParam(required = false) Map param) {
        return xtCityService.findCacheAll(param);
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        return xtCityService.getPageList(param);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) XtCityPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("城市新增参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("城市新增成功!");

        int affectRows = xtCityService.insert(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("城市新增失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true) XtCityPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("城市更新参数错误");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("城市更新成功!");
        int affectRows = xtCityService.update(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("城市更新失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestParam(required = true) Map map) throws Exception {
        String id = DataTypeUtil.toNotNullString(map.get("id"));
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("城市删除参数错误");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("城市删除成功!");
        int affectRows = xtCityService.delete(id);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("城市删除失败!");
        }
        return baseResultMap;
    }

}
